import { defineStore } from 'pinia'
import {
  IBlog, IBlogCategory, IBlogComment,
  IBlogLatest, IBlogNestedComment, IBlogStats, INewBlog, IQueryBlog
} from '@/types/blog'
import {
  addBlogApi, addBlogCommentApi, deleteBlogApi, queryAdminBlogApi, queryAdminBlogByIdApi, queryAdminBlogCategoryApi,
  queryBlogApi, queryBlogByIdApi,
  queryBlogCommentApi, queryBlogStatsApi, queryLatestBlogApi, updateBlogApi
} from '@/apis/blog.ts'
import { IHandleApi } from 'vue3-common/types'
import { ElMessage } from 'element-plus'
import { ImagePathEnum } from '@/types'
import { flattenToTwoLevel } from '@/utils/blog/blogUtil.ts'

export const useBlogStore = defineStore('blog', {
  state: () => ({
    blogList: [] as IBlog[],
    blogCommentList: [] as IBlogComment[],
    blogNestedCommentList: [] as IBlogNestedComment[],
    currentBlogComment: {
      id: 0,
      blogId: 0,
      name: '',
      website: '',
      content: '',
      ipAddress: '',
      userAgent: '',
      parentId: 0,
      isApproved: true,
      createTime: ''
    } as IBlogComment,
    currentBlogId: 0,
    currentBlog: {
      id: 0,
      title: '',
      topValue: 0,
      isGreat: false,
      categoryId: 0,
      category: '',
      summary: '',
      content: '',
      wordCount: 0,
      readDuration: 0,
      visitCount: 0,
      createTime: '',
      updateTime: ''
    } as IBlog,
    newBlog: {} as INewBlog,
    blogCategoryList: [] as IBlogCategory[],
    blogStats: {} as IBlogStats,
    latestBlogList: [] as IBlogLatest[],
    queryBlog: {} as IQueryBlog,
    blogImagePath: ImagePathEnum.BLOG,
    blogApiType: 'ADD' as IHandleApi,
    pageInfo: {
      currentPage: 1,
      pageSize: 10,
      totalSize: 1
    },
    isScrollEnd: false,
    adminPageInfo: {
      currentPage: 1,
      pageSize: 10,
      totalSize: 1
    },
    progressValue: '0%'
  }),
  actions: {
    async queryBlog() {
      const { currentPage, pageSize } = this.pageInfo
      const result = await queryBlogApi(currentPage, pageSize)
      this.blogList = result.records
      this.pageInfo.totalSize = result.total
    },
    async queryAdminBlog() {
      const { currentPage, pageSize } = this.adminPageInfo
      const result = await queryAdminBlogApi(currentPage, pageSize)
      this.blogList = result.records
      this.pageInfo.totalSize = result.total
    },
    async queryBlogById(id: number) {
      this.currentBlog = await queryBlogByIdApi(id)
      await this.queryBlogComment(this.currentBlogId)
      this.blogNestedCommentList = flattenToTwoLevel(this.blogCommentList)
    },
    async queryAdminBlogById(id: number) {
      this.currentBlog = await queryAdminBlogByIdApi(id)
    },
    async queryBlogComment(blogId: number) {
      this.blogCommentList = await queryBlogCommentApi(blogId)
    },
    async addBlogComment(blogComment: IBlogComment) {
      await addBlogCommentApi(blogComment)
      await this.queryBlogById(blogComment.blogId)
    },
    async queryAdminBlogCategory() {
      this.blogCategoryList = await queryAdminBlogCategoryApi()
    },
    async queryBlogStats() {
      this.blogStats = await queryBlogStatsApi()
    },
    async queryLatestBlog() {
      this.latestBlogList = await queryLatestBlogApi()
    },
    async handleBlogApi(id?: number) {
      switch (this.blogApiType) {
        case 'ADD':
          await addBlogApi(this.newBlog)
          ElMessage.success('新增博客成功')
          break
        case 'UPDATE':
          await updateBlogApi(this.newBlog.id as number, this.newBlog)
          ElMessage.success('更新博客成功')
          break
        case 'DELETE':
          await deleteBlogApi(id as number)
          ElMessage.success('删除博客成功')
          break
        default:
          break
      }
      await this.refreshInfo()
    },
    async refreshInfo() {
      this.pageInfo.currentPage = 1
      await this.queryBlog()
      await this.queryBlogStats()
      await this.queryLatestBlog()
    },
    getEmptyBlog(): INewBlog {
      return {
        id: 0,
        category: '',
        content: '',
        isGreat: false,
        title: '',
        topValue: 0
      }
    }
  }
})
